<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/20
 * Time: 18:59
 */

namespace backend\controllers;


use common\controllers\BackendCommonCtrl;
use common\services\AreaSve;
use common\services\MemberSve;
use common\services\WorkSve;
use common\services\WorksClassSve;
use common\services\PersonnelClassSve;
use Yii;
use yii\helpers\Url;
use common\library\comFun;
use moonland\phpexcel\Excel;

class MemberController extends BackendCommonCtrl
{
    /**
     * 获取用户列表 BY:lihouse
     * @return string
     */
    public function actionList()
    {
        $request = Yii::$app->request;
        $MemberSve = new MemberSve();
        $status = $request->get('status',1);
        $rs = $MemberSve->getList([['=', 'is_del', 0],['=','status',$status]], 'register_time DESC', true);

        //角色列表
        $PersonnelClassSve = new PersonnelClassSve();
        $roleList = $PersonnelClassSve->getList([], 'sort DESC,personnel_class_id ASC', true);
        $showRole = [];
        foreach ($roleList as $key => $value) {
            $showRole[$value['personnel_class_id']] = $value['class_name'];
        }
        $rs['data']['roleList'] = comFun::formatTree($roleList, 'personnel_class_id');
        $rs['data']['showRole'] = $showRole;
        //城市列表
        $cityList = (new AreaSve())->cityListToArr();
        $rs['data']['cityList'] = $cityList;
        $rs['data']['status'] = $status;
        return $this->render('list', $rs['data']);
    }

    /**
     * 添加用户
     * @return string
     */
    public function actionAdd()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            $MemberSve = new MemberSve();
            $data = Yii::$app->request->post();
            $rs = $MemberSve->add($data);
            if ($rs['code'] === 0) {
                $rs['data']['redirect'] = Url::toRoute('/member/list');
            }
            return $this->asJson($rs);
        } else {
            //补全数据
            $cityList = (new AreaSve())->cityListToArr();
            $PersonnelClassSve = new PersonnelClassSve();
            $roleList = $PersonnelClassSve->getList([], 'sort DESC,personnel_class_id ASC', true);
            return $this->render('add', ['info' => [], 'cityList' => $cityList, 'roleList' => comFun::formatTree($roleList, 'personnel_class_id')]);
        }
    }


    /**
     * 获取用户信息 BY:lihouse
     * @return string
     */
    public function actionInfo()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            $MemberSve = new MemberSve();
            $data = Yii::$app->request->post();

            $rs = $MemberSve->update($data);
            if ($rs['code'] === 0) {
                $rs['data']['redirect'] = Url::toRoute('/member/list');
            }
            return $this->asJson($rs);
        } else {
            $MemberSve = new MemberSve();
            $memberId = $request->get('member_id');
            $rs = $MemberSve->getInfo(['member_id' => $memberId]);
            //补全数据
            $cityList = (new AreaSve())->cityListToArr();
            $PersonnelClassSve = new PersonnelClassSve();
            $roleList = $PersonnelClassSve->getList([], 'sort DESC,personnel_class_id ASC', true);
            return $this->render('info', ['info' => $rs['data']['Obj'], 'cityList' => $cityList, 'roleList' => comFun::formatTree($roleList, 'personnel_class_id')]);
        }
    }


    /**
     * 删除用户 BY:lihouse
     * @return \yii\web\Response
     */
    public function actionDel()
    {
        $request = Yii::$app->request;
        $MemberSve = new MemberSve();
        $memberId = $request->get('member_id');
        $rs = $MemberSve->del(['member_id' => $memberId]);
        if ($rs['code'] === 0) {
            $rs['data']['redirect'] = Url::toRoute('member/list');
        }
        return self::asJson($rs);
    }


    /**
     * 导出数据
     * @return string
     */
    public function actionOutInfo()
    {
        $request = Yii::$app->request;
        if ($request->isPost) {
            $member_id = $request->post('member_id');
            $MemberSve = new MemberSve();
            $memberInfo = $MemberSve->getInfo(['member_id'=>$member_id]);

            $start_time = strtotime($request->post('start_time'));
            $end_time = strtotime($request->post('end_time')) + 86399;
            $work_class_id = $request->post('work_class_id');
            $WorkSve = new WorkSve();
            $where1 = ['and', ['>', 'start_time', $start_time], ['<', 'start_time', $end_time], ['=', 'member_id', $member_id]];
            $where2 = ['and', ['>', 'end_time', $start_time], ['<', 'end_time', $end_time], ['=', 'member_id', $member_id]];
            if ($work_class_id) {
                array_push($where1, ['=', 'work_class_id', $work_class_id]);
                array_push($where2, ['=', 'work_class_id', $work_class_id]);
            }

            $dataList = $WorkSve->getList(['or', $where1, $where2], 'start_time DESC,end_time ASC,work_id DESC');
            if ($dataList['code'] === 0 && !empty($dataList['data']['list'])) {
                $data = [];
                foreach ($dataList['data']['list'] as $key=>$value){
                    $tep = [];
                    $tep['member_name'] = $value['member_name'];
                    $tep['work_class_name'] = $value['work_class_name'];
                    $tep['start_time'] = date('Y-m-d',$value['start_time']);
                    $tep['end_time'] = date('Y-m-d',$value['end_time']);
                    $tep['remark'] = $value['remark'];
                    array_push($data,$tep);
                }
                Excel::widget([
                    'fileName' => $memberInfo['data']['Obj']->nickname.'去向数据数据' . date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']),
                    'format' => 'Excel2007',
                    'mode' => 'export',
                    'models' => $data,
                    'columns' => [
                        'member_name',
                        'work_class_name',
                        'start_time',
                        'end_time',
                        'remark'],
                    'headers' => [
                        'member_name' => '人员名称',
                        'work_class_name' => '去向',
                        'start_time' => '开始时间',
                        'end_time' => '结束时间',
                        'remark' => '备注']
                ]);

            } else {

            }

        }
        $member_id = $request->get('member_id');
        $WorksClassSve = new WorksClassSve();
        $worksClass = $WorksClassSve->getList([]);
        return $this->render('out-info', ['member_id' =>$member_id, 'worksClass' => $worksClass['data']['list']]);

    }
}